<?php
class Page_model extends MY_Model
{
	public
		$table = 'pages',
		$prKey = 'id',
		$publishedField = 'status',

		$status = array(
			'1' => 'Active',
			'2' => 'Blocked',
		),
		
		$category = array(
			'1' => 'Static Pages',
			'2' => 'News Pages',
		)
		
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 			=> '',
			'title'			=> T_('Title'),
			'teaser'		=> T_('Teaser'),
			'alias'			=> T_('Alias'),
			'category'		=> T_('Category'),
			'text' 			=> T_('Full Text'),
			'meta_title'	=> T_('Meta Title'),
			'meta_keywords'	=> T_('Meta Keywords'),
			'meta_description' => T_('Meta Description'),
			'date_created'	=> T_('Date Creation'),
			'date_modified'	=> T_('Date Modified'),
			'id_admins'		=> T_('Moderator'),
			'status' 		=> T_('Status'),
		);
			
		$this->rules = array(
			'id' 			=> "trim|integer",
			'title' 		=> "trim|required|max_length[255]",
			'teaser'		=> "trim",
			'alias' 		=> "trim|required|max_length[255]",
			'category'		=> "trim|required|is_natural_no_zero",
			'text' 			=> "trim|required",
			'meta_title' 	=> "trim|strip_tags|max_length[255]",
			'meta_keywords'	=> "trim|strip_tags|max_length[1000]",
			'meta_description' => "trim|strip_tags|max_length[1000]",
			'date_created'	=> "trim|max_length[255]",
			'date_modified'	=> "trim|max_length[255]",
			'id_admins'		=> "trim|integer",
			'status' 		=> "trim|required|integer",
		);
	}
	
	function search($config = array())
	{
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');

		$sql = '';
		$sql .= ' FROM '.$this->table.' p';
		$sql .= ' LEFT JOIN admins a ON p.id_admins=a.id';
		$sql .= ' WHERE 1 = 1 AND p.category='.$this->id_category; 
								
		if ($search)
		{
			$sql .= ' AND p.title LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by p.page_created'; 
				break;
			
			default:
				$sql .= ' ORDER BY p.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		$sql = 'SELECT p.*, CONCAT(a.first_name, " ", a.last_name) AS admin_name' . $sql; 

		$this->data['list'] = $this->db->query($sql)->result();
		$this->data['search'] = $search;
	}

	function get($id = null)
	{	
		$this->db->from($this->table);
		//$this->db->join('admins', $this->table.'.id_admins = admins.id', 'left');
		$this->db->where($this->table.'.id', $id);
		$query = $this->db->get();

		return $query->row_array();
	}
	
	function getByAlias($alias = 'all')
	{
                $this->db->select('*');
		$this->db->from('pages');
		//$this->db->where('category', $this->id_category);
		$this->db->where('status', 1);
		
		if ($alias != 'all')
		{
                	$this->db->where('alias', $alias);
                        return $this->db->get()->row();
		}
		else
		{
                        $this->db->order_by('date_created', 'desc');
			return $this->db->get()->result();
		}
	}		

	function getNewsBlock($category = PAGE_CATEGORY_ADVERTISER)
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('category', $category);
		$this->db->where('status', 1);
		$this->db->limit(3);
		$this->db->order_by('date_created', 'desc'); 				

		return $this->db->get()->result();
	}		
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$data = $this->page_model->get($id);
			
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Page was not removed.'));
				$report .= ",0"; // don't deleted
			}
			else
			{
				$this->log->writeMessage('done', T_('Page was removed.'));
				$report .= ",1"; // deleted								
			}							
		}
		
		return substr($report, 1);
	}
		
	function insert($data)
	{
		if (!empty($data))
		{
			$data['date_created'] = MYSQL_NOW();
			
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Page was added succesfully.'));
				
				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Page was not created.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Saved succesfully.'));

			return TRUE;			
		}		
	}

    //first efforts to grab faq information
   function faq($topic)
   {
        /*$request = "SELECT ".$topic." FROM my_table LIMIT 1";
        $row = $query->row_array();
        echo $row['name'];*/
   }
}
?>